var handling = {
    init : function(){
        // 取消订单按钮事件
        this.cancelEvent($('.cancel-btn'));
        this.checkRule($('#check-policy'));
        this.closeLayer($('.policy .popup-mask, .policy .confirm-btn, .confirm .no-btn'));
        // 确认取消订单
        this.confirmEvent($('.confirm .confirm-btn'));
    },

    formSubmit : function(){
        $('#form').submit(function(e){
            var params = $('#form').serialize();
            $.ajax({
                url: location.origin + BT.context('url'),
                type: "POST",
                data: params,
                success: function(resp) {
                     var info = JSON.parse(resp);
                    if(info){
                        if(info.code == 0){
                            location.reload();
                        }else{
                            alert(info.message);
                        }
                    }
                    $this.removeClass('push');
                },
                error: function(resp) {
                    alert('对不起，订单取消失败！');
                }
            });
            return false;
        });
        $('#form').submit();
    },

    confirmEvent : function($dom){
        var self = this;
        $dom.on('click', function(){
            $this = $(this);
            if($this.hasClass('push')) return;
            $this.addClass('push');
            setTimeout(function(){
                $('.popup').addClass('hide');
                $this.removeClass('push');
            }, 100);
            self.formSubmit();
        });
    },

    cancelEvent : function($dom){
        $dom.on('click', function(){
            var $this = $(this);
            $this.addClass('push');
            setTimeout(function(){
                $this.removeClass('push');
            }, 100);
            $('.popup.confirm').removeClass('hide');
        }); 
    },

    checkRule : function($dom){
        $dom.on('click', function(){
            $('.popup.policy').removeClass('hide');
            // 浮层样式设置
            var windowHeight = window.screen.availHeight;
            var $inner = $('#popup-comfirm').find('.popup-inner');
            $inner.css('top', (windowHeight - $inner.height()) / 2);
        });
    },

    closeLayer : function($dom){
        $dom.on('click', function(){
            var $this = $(this);
            if($this.hasClass('confirm-btn') || $this.hasClass('no-btn')) $this.addClass('push');
            setTimeout(function(){
                $this.removeClass('push');
                $('.popup').addClass('hide');
            }, 100); 
        });
    }
}
$(function(){
    handling.init();
});